package edu.gatech.gitmad;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKeyFactory;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class SecureMemoActivity extends Activity{
	private static String TAG = "SecureMemoActivity";
	private static String SECRET_KEY = "all1337knociaunuyau78qy6a8mal8u4yti7a47"; 
	private static String PREFS_NAME = "preferences";
	EditText editTextFirst;
	EditText editTextSecond;
	String fileName;
	String memo;
	EditText editTextThird;
	String password;
	String[] data;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    }
    
    public void encryptPref(View v){
    	Log.i(TAG,"encryptPref now called");
    	
    	getNameMemPass();
    
    	//put the password in the settings file
    	SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(fileName, password);
        editor.commit();

    	writeFileBad(memo);
		    	
    }
    
    private String[] getNameMemPass() {
		editTextFirst = (EditText)findViewById(R.id.editText1);
    	editTextSecond=(EditText)findViewById(R.id.editText2);
    	editTextThird = (EditText)findViewById(R.id.editText3);
    	
    	fileName = editTextFirst.getText().toString();
    	memo = editTextSecond.getText().toString();
    	password = editTextThird.getText().toString().substring(0,4);
    	
    	String[] vals = {fileName,memo,password};
    	
    	
		return vals;
	}

	private void writeFileBad(String memo) {
		FileOutputStream fos;
    	   	
		try {
			fos = openFileOutput(fileName, Context.MODE_PRIVATE);
			fos.write(memo.getBytes());
			fos.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
			Log.e(TAG,"error");
		} catch (Exception e){
			e.printStackTrace();
			Log.e(TAG,"error");
		}
	}

	public void encryptKey(View v){
    	Log.i(TAG,"encryptKey now called");
    	
    	getNameMemPass();    	
    	
    	//get the hash from the crypto lab and make it a string
    	//byte[] preHash = CryptoLaboratory.makeHash(password.toCharArray(),SECRET_KEY.getBytes());
    	
    	password = password + SECRET_KEY;
    	
    	String saveSafe = hasher();    	
    	
    	savePreferences(fileName, saveSafe);
        
        writeFileBad(memo);
    	
    }
	private String hasher() {
		byte [] defaultBytes = password.getBytes();
    	MessageDigest algorithm;
		try {
			algorithm = MessageDigest.getInstance("MD5");
		
	    	algorithm.reset();
	    	algorithm.update(defaultBytes);
	    	byte preHash[] = algorithm.digest();
	    	
	    	String saveSafe = convert(preHash);  
	    	return saveSafe;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//should never get here
		return fileName;
	}
    
	//This is written by taylor wrobel to convert a byte array to a string in hex
    public static String convert(byte[] b){
		char[] hex = new char[b.length*2];
		for(int i = 0; i < b.length; i++){
			byte l = (byte)(b[i] & 0xf);
			byte f = (byte)((b[i] >> 4) & 0xf);
			l += (l < 10 ? 48 : 87);//Sexy ternary operator 
			f += (f < 10 ? 48 : 87);
			hex[2*i+1] = (char)l;
			hex[2*i] = (char)f;
		}
		return new String(hex);
	}
    
	private void savePreferences(String fileName, String saveSafe) {
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(fileName, saveSafe);
        editor.commit();
	}
    
    public void encryptHash(View v){
    	Log.i(TAG,"encryptHash now called");
    	
    	getNameMemPass();
    	
    	String saveSafe = hasher();
    	
    	savePreferences(fileName,saveSafe);
        writeFileBad(memo);
    }
    
    public void encryptLog(View v){
    	Log.i(TAG,"encryptLog now called");
    	
    	getNameMemPass();
    	
    	String saveSafe = hasher();
    	Log.v(TAG,"this is the password: "+ saveSafe + ":" +password + " This is the file: " + fileName);
    	savePreferences(fileName,saveSafe);
        writeFileBad(memo);
    }    

}